﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MvcEngine.Core.Repositories;
using MvcEngine.Core;
using System.Data.Linq;
using MvcEngine.Core.Content;

namespace MvcEngine.LinqToSqlProvider
{
    public class ModuleRepository : Repository, IModuleRepository
    {
        public Module Get(string name)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Modules().Single(m => m.Name == name);
            }
        }

        public void Delete(string name)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.Modules().DeleteOnSubmit(contextManager.Context.Modules().Single(m => m.Name == name));
                contextManager.Commit();
            }
        }

        public Module Get(Guid id)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Modules().SingleOrDefault(m => m.Id == id);
            }
        }

        public IList<Module> GetAll()
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Modules().ToList();
            }
        }

        public IList<Module> GetAll(bool isInstallable)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.Modules().Where(m => m.IsInstallable == isInstallable).ToList();
            }
        }

        public void Add(Module entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(Guid id)
        {
            throw new NotImplementedException();
        }

        public void DeleteAll()
        {
            throw new NotImplementedException();
        }
    }
}
